//
//  NewRelease.swift
//  亿享_04
//
//  Created by ibokan on 2017/7/16.
//  Copyright © 2017年 ibokan. All rights reserved.
//

import UIKit
import LeanCloud
import AVOSCloud
import MapKit

class NewRelease: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate,UITextFieldDelegate,CLLocationManagerDelegate {
    
    @IBOutlet weak var Rtype: UISegmentedControl!
    @IBOutlet weak var Rexplain: UITextField!
    @IBOutlet weak var Rrental: UITextField!
    @IBOutlet weak var Usite: UITextField!
    @IBOutlet weak var Rname: UITextField!
    @IBOutlet weak var imgs: UIButton!
    var goods: AVFile!
    //定位管理器
    var latitude:Double = 0.0
    var longitude:Double = 0.0
    var locationManager:CLLocationManager = CLLocationManager()
    // var imgFile =
    override func viewDidLoad() {
        super.viewDidLoad()
        Rname.delegate = self
        Usite.delegate = self
        Rrental.delegate = self
        Rexplain.delegate = self
        //设置定位服务管理器代理
        locationManager.delegate = self
        //设置定位进度
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //更新距离
        locationManager.distanceFilter = 100
        ////发送授权申请
        locationManager.requestAlwaysAuthorization()
        if (CLLocationManager.locationServicesEnabled())
        {
            //允许使用定位服务的话，开启定位服务更新
            locationManager.startUpdatingLocation()
            print("定位开始")
        }
        
    }
    
    @IBAction func fromAlbum(sender: AnyObject) {
        let imagePicker = UIImagePickerController()
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePicker.sourceType = .camera
        } else {
            imagePicker.sourceType = .photoLibrary
        }
        
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)
    }
    //选择图片成功后代理
    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [String: Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imgs.setImage(image, for: .normal)
        self.goods = AVFile(data: UIImagePNGRepresentation(image)!)
        dismiss(animated: true, completion: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        Rname.resignFirstResponder()
        Usite.resignFirstResponder()
        Rrental.resignFirstResponder()
        Rexplain.resignFirstResponder()
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool{
        Rname.resignFirstResponder()
        Usite.resignFirstResponder()
        Rrental.resignFirstResponder()
        Rexplain.resignFirstResponder()
        return true
    }
    // 获取 经纬度
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let cr:CLLocation = locations.last!
        self.latitude = cr.coordinate.latitude
        self.longitude = cr.coordinate.longitude
    }
    @IBAction func btnOK(_ sender: Any) {
        if (goods==nil) {
            let alert = UIAlertController(title: "Alert", message: "请选择展示图！", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }else if (Rname.text == "") {
            let alert = UIAlertController(title: "提示", message: "请填写标题", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }else if (Usite.text == "") {
            let alert = UIAlertController(title: "提示", message: "请填写详细地址", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }else{
            let Release = LCObject(className: "Release")
            Release.set("Rname", value: Rname.text)
            Release.set("Usite", value: Usite.text)
            let rtype = Rtype.selectedSegmentIndex==0 ? "免费" : "出租"
            Release.set("Rtype", value: rtype)
            let rental = Rrental.text! + "元/天"
            Release.set("Rrental", value: rental)
            Release.set("Rexplain", value: Rexplain.text)
            Release.set("Ulatitude", value: self.latitude)
            Release.set("Ulongitude", value: self.longitude)
            if Release.save().isSuccess {
                let releaseOld = AVObject.init(className: "Release", objectId: (Release.objectId?.value)!)
                releaseOld.setObject(goods, forKey: "Rimg")
                releaseOld.save()
                navigationController?.popViewController(animated: true)
            } else {
                let alert = UIAlertController(title: "Alert", message: "失败", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                self.present(alert, animated: true, completion: nil)
            }
            
        }
        
    }
    
}

